<template>
  <div>
    <el-divider>用印信息</el-divider>
    <el-form-item style="margin-left: 0">
      <el-row type="flex" :gutter="20" align="middle">
        <el-col :span="6">
          <el-form-item label="用印类别" label-width="100px">
            <el-select v-model="reportCommonObj.stampCategory" style="width: 100%">
              <el-option :value="1" label="电子章" />
              <el-option :value="2" label="实体章" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="用印份数">
            <el-input-number v-model="reportCommonObj.stampNumber" :min="0" :max="100" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="用印类别" label-width="100px">
            <el-select v-model="reportCommonObj.financeStampCategory" style="width: 100%">
              <el-option :value="1" label="电子章" />
              <el-option :value="2" label="实体章" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="用印份数">
            <el-input-number v-model="reportCommonObj.financeStampNumber" :min="0" :max="100" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" :gutter="20" align="middle">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="stampRecordDialogVisibleOpenHandler">用印记录</el-button>
        </el-col>
      </el-row>
    </el-form-item>
    <el-dialog
      title="提示"
      :visible.sync="stampRecordDialogVisible"
      destroy-on-close
      width="50%"
      :before-close="stampRecordDialogVisibleBeforeCloseHandler"
    >
      <el-button type="success" size="small" @click="addStampRecordDialogOpenHandler">添加记录</el-button>
      <el-table :data="stampRecordList">
        <el-table-column label="用印类别">
          <template v-slot="scope">
            <span v-if="scope.row.stampCategory == '1'">电子章</span>
            <span v-if="scope.row.stampCategory == '2'">实体章</span>
          </template>
        </el-table-column>
        <el-table-column label="用印份数">
          <template v-slot="scope">
            {{ scope.row.stampNumber }}
          </template>
        </el-table-column>
        <el-table-column label="用印时间" prop="stampTime" :formatter="tableTimeFormat" />
        <el-table-column label="用印备注">
          <template v-slot="scope">
            {{ scope.row.stampRemark }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="text" size="small" @click="updateStampRecordDialogOpenHandler(scope.row.id)">编辑</el-button>
            <el-button type="text" size="small" @click="deleteStampRecordHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <el-dialog
      title="提示"
      :visible.sync="addStampRecordDialogVisible"
      destroy-on-close
      width="30%"
      :before-close="addStampRecordDialogVisibleBeforeCloseHandler"
    >
      <el-form size="small" :model="stampRecordObj">
        <el-form-item label="用印类别">
          <el-select v-model="stampRecordObj.stampCategory">
            <el-option :value="1" label="电子章" />
            <el-option :value="2" label="实体章" />
          </el-select>
        </el-form-item>
        <el-form-item label="用印份数">
          <el-input-number v-model="stampRecordObj.stampNumber" :min="1" :max="100" label="描述文字" /> 次
        </el-form-item>
        <el-form-item label="用印时间">
          <el-date-picker
            v-model="stampRecordObj.stampTime"
            value-format="timestamp"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="备注" label-width="40px">
          <el-input v-model="stampRecordObj.stampRemark" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addOrUpdateStampRecord">保存</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { parseTime } from '@/utils'
import {
  getStampRecordPage,
  getStampRecordById,
  deleteStampRecord,
  addStampRecord,
  updateStampRecord
} from '@/api/administrativeInfo'

export default {
  name: 'AdministrativeStampInfo',
  props: {
    reportCommonObj: {
      type: Object,
      required: true
    },
    isAudit: {
      type: Boolean,
      default: false,
      required: true
    },
    auditLeaf: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      stampRecordDialogVisible: false,
      stampRecordList: [],
      addStampRecordDialogVisible: false,
      stampRecordObj: {}
    }
  },
  computed: {
    ...mapGetters([
      'id'
    ])
  },
  methods: {
    stampRecordDialogVisibleOpenHandler() {
      this.getStampRecordPage()
      this.stampRecordDialogVisible = true
    },
    addStampRecordDialogVisibleBeforeCloseHandler() {
      this.addStampRecordDialogVisible = false
    },
    getStampRecordPage() {
      const id = this.$route.query.id
      getStampRecordPage(id).then(res => {
        console.log(res)
        this.stampRecordList = res.data.list
      })
    },
    stampRecordDialogVisibleBeforeCloseHandler() {
      this.stampRecordDialogVisible = false
    },
    addStampRecordDialogOpenHandler() {
      this.stampRecordObj = {}
      this.addStampRecordDialogVisible = true
    },
    updateStampRecordDialogOpenHandler(id) {
      getStampRecordById(id).then(res => {
        this.stampRecordObj = res.data.obj
        this.addStampRecordDialogVisible = true
      })
    },

    deleteStampRecordHandle(id) {
      this.$confirm('此操作将删除收费记录', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        deleteStampRecord(id).then(res => {
          this.$message.success(res.message)
          this.getStampRecordPage()
        })
      })
    },
    addOrUpdateStampRecord() {
      const stampRecordObj = this.stampRecordObj
      stampRecordObj.reportId = this.$route.query.id
      if (stampRecordObj.id) {
        // 修改
        updateStampRecord(stampRecordObj).then(res => {
          this.$message.success(res.message)
          this.getStampRecordPage()
        })
      } else {
        // 新增
        addStampRecord(stampRecordObj).then(res => {
          this.$message.success(res.message)
          this.getStampRecordPage()
        }).finally(() => {
          this.stampRecordObj = {}
        })
      }
    },
    tableTimeFormat(row, column, cellValue, index) {
      return parseTime(cellValue, '')
    }
  }
}
</script>

<style scoped>

</style>
